﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.BLL
{
    /// <summary>
    /// 文档 逻辑类
    /// </summary>
    public partial class Document
    {
        /// <summary>
        /// 文档数据类
        /// </summary>
        private DAL.Document documentDAL = new DAL.Document();
        /// <summary>
        /// 获得单个Office文件路径
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decDocId"></param>
        /// <returns></returns>
        public string getDocSrvFileNameByOffice(string strEntityNo, decimal decDocId)
        {
            return documentDAL.GetDocSrvFileNameByOffice(strEntityNo, decDocId);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="fOrderFiled"></param>
        /// <returns></returns>
        public List<ElingOA6_Admin_DSJCJ.Model.Document> GetListByPage(string tableName, string fields, int pageSize, int pageIndex, string strWhere, string fOrderFiled, int orderType, out int total)
        {
            DataSet ds = documentDAL.GetDocumentListByPage(tableName, fields, pageSize, pageIndex, strWhere, fOrderFiled, orderType, out total);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获取计划总结上报情况
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public DataSet GetPlanSummaryReport(string year)
        {
            return documentDAL.GetPlanSummaryReport(year);
        }

                /// <summary>
        /// 获取所有的年份，按年份降序
        /// <creator>zhongyl</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public DataSet GetYeat(string strEntityNo)
        {
            return documentDAL.GetYeat(strEntityNo);
        }

        /// <summary>
        /// 获得单个缩略图路径
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decDocId"></param>
        /// <returns></returns>
        public string getDocFThumbSrvFileName(string strEntityNo, decimal decDocId)
        {
            return documentDAL.GetDocFThumbSrvFileName(strEntityNo, decDocId);
        }

        /// <summary>
        /// 信息还原
        /// <creator>wangl</creator>
        /// <date>2013-08-20</date> 
        /// </summary>
        /// <param name="docIdList">信息编号</param>
        public void UpdateDocRestore(List<decimal> docIdList)
        {
            documentDAL.UpdateDocRestore(docIdList);
        }

        /// <summary>
        /// 删除信息和附件(事物模式)
        /// <creator>wangl</creator>
        /// <date>2013-08-20</date>
        /// </summary>
        /// <param name="docIdList">信息编号列表</param>
        /// <param name="entityNo">公司编号</param>
        public void DelDocumentAndAttach(List<decimal> docIdList, string entityNo)
        {
            documentDAL.DelDocumentAndAttach(docIdList, entityNo);
        }


        /// <summary>
        /// 获取某个员工所有放入回收站的信息的ID FRecycle='Y'
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public List<decimal> getDocIDByFRecycle(string strEntityNo, string strEmplNo)
        {
            return documentDAL.GetDocIDByFRecycle(strEntityNo, strEmplNo);
        }

        /// <summary>
        /// 获得多个Office文件路径
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decDocId"></param>
        /// <returns></returns>
        public List<string> getDocSrvFileNameByOffice(string strEntityNo, string strDocIdS)
        {
            return documentDAL.GetDocSrvFileNameByOffice(strEntityNo, strDocIdS);
        }

        /// <summary>
        /// 获取多条记录的附件路径
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decAttachSource"></param>
        /// <param name="strAttachType"></param>
        /// <returns></returns>
        public List<string> getDocAttachS(string strEntityNo, string strAttachSourceS, string strAttachType)
        {
            return documentDAL.GetDocAttachS(strEntityNo, strAttachSourceS, strAttachType);
        }


        /// <summary>
        /// 获取单条记录的附件路径
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decAttachSource"></param>
        /// <param name="strAttachType"></param>
        /// <returns></returns>
        public List<string> getDocAttachS(string strEntityNo, decimal decAttachSource, string strAttachType)
        {
            return documentDAL.GetDocAttachS(strEntityNo, decAttachSource, strAttachType);
        }
        /// <summary>
        /// 获得多个缩略图路径
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decDocId"></param>
        /// <returns></returns>
        public List<string> getDocFThumbSrvFileName(string strEntityNo, string strDocIdS)
        {
            return documentDAL.GetDocFThumbSrvFileName(strEntityNo, strDocIdS);
        }

        /// <summary>
        /// 根据信息编号获取信息条数
        /// <creator>wangl</creator>
        /// <date>2013-07-22</date> 
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="docClsCd">信息类别编号</param>
        /// <returns></returns>
        public int GetDocCountByDocClsCd(string entityNo, string docClsCd)
        {
            return documentDAL.GetDocCountByDocClsCd(entityNo, docClsCd);
        }
        /// <summary>
        /// 根据信息编号获取信息编号(适用于单信息)
        /// <creator>wangl</creator>
        /// <date>2013-09-10</date> 
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="docClsCd">信息类别编号</param>
        /// <returns></returns>
        public decimal GetDocIdDocClsCd(string entityNo, string docClsCd)
        {
            return documentDAL.GetDocIdDocClsCd(entityNo, docClsCd);
        }
        /// <summary>
        /// 获取文档实体类
        /// <creator>wangl</creator>
        /// <date>2013-08-07</date> 
        /// </summary>
        /// <param name="docId">文档编号</param>
        /// <returns>文档实体类</returns>
        public Model.Document GetModel(decimal docId)
        {
            return documentDAL.GetModel(docId);
        }
        /// <summary>
        ///  保存信息同时保存附件,通知对象(事务模式)
        /// <creator>wangl</creator>
        /// <date>2013-08-13</date> 
        /// </summary>
        /// <param name="documentModel">信息实体类</param>
        /// <param name="docAttachList">附件列表</param>
        /// <param name="docRecObjectList">通知对象列表</param>
        /// <param name="imgNewPath">文件新名称</param>
        /// <param name="extenSion">扩展名</param>
        /// <param name="documentUnionPubEmplList">合发部门列表</param>
        /// <param name="docReferenceList ">呈送信息栏目列表</param>
        /// <returns>文档编号</returns>
        public decimal SaveDocumentAndAttach(Model.Document documentModel, List<Model.DocAttach> docAttachList,
            List<Model.DocRecObject> docRecObjectList, List<Model.DocumentUnionPubEmpl> documentUnionPubEmplList, List<Model.DocReference> docReferenceList,
            List<Model.documentAuthority> documentAuthorityList)
        {
            string imgNewPath = "";//文件名称前的路径
            string extenSion = "";//文件类型 带.      

            string strImgPath = documentModel.FSvrFileName;
            if (null != strImgPath)
            {
                int t = strImgPath.LastIndexOf('/');
                if (t != -1)
                {
                    imgNewPath = strImgPath.Substring(0, t + 1);
                    t = strImgPath.LastIndexOf('.');
                    if (t != -1)
                    {
                        string strTitle = documentModel.FTitle.Replace(":", "").Replace("：", "").Replace(" ", "");//office控件的名称中不能有:号
                        extenSion = strTitle + strImgPath.Substring(t);
                    }
                }
            }
            return documentDAL.SaveDocumentAndAttach(documentModel, docAttachList, docRecObjectList, imgNewPath, extenSion, documentUnionPubEmplList, docReferenceList,documentAuthorityList);
        }
        /// <summary>
        /// 修改信息和附件,通知对象，包括信息置顶改变修改相应置顶信息(事物模式)
        /// <creator>wangl</creator>
        /// <date>2013-08-15</date> 
        /// </summary>
        /// <param name="documentModel">信息实体类</param>
        /// <param name="docAttachList">附件列表</param>
        /// <param name="docRecObjectList">通知对象列表</param>
        /// <param name="isTop">是否置顶</param>
        /// <param name="documentUnionPubEmplList">合发部门列表</param>
        /// <param name="docReferenceList ">呈送信息栏目列表</param>
        public void UpdateDocAndAttach(Model.Document documentModel, List<Model.DocAttach> docAttachList, List<Model.DocRecObject> docRecObjectList, string isTop, List<Model.DocumentUnionPubEmpl> documentUnionPubEmplList, List<Model.DocReference> docReferenceList,
            List<Model.documentAuthority> documentAuthorityList)
        {
            documentDAL.UpdateDocAndAttach(documentModel, docAttachList, docRecObjectList, isTop, documentUnionPubEmplList, docReferenceList, documentAuthorityList);
        }
        /// <summary>
        /// 更新数据的信息类别
        /// <creator>wangl</creator>
        /// <date>2013-08-20</date> 
        /// <param name="docId">信息编号</param>
        /// <param name="docClsCd">信息类别编号</param>
        /// </summary>
        public void UpdateDocClsCd(decimal docId, string docClsCd)
        {
            documentDAL.UpdateDocClsCd(docId, docClsCd);
        }
        /// <summary>
        /// 信息置顶,并修改该类下其他信息的置顶顺序(事物模式)
        /// </summary>
        /// <param name="docIdList">信息编号列表</param>
        /// <param name="isTop">置顶 1-是 0-否</param>
        public void UpdateTop(List<decimal> docIdList, string isTop)
        {
            documentDAL.UpdateTop(docIdList, isTop);
        }
        /// <summary>
        /// 获得数据列表
        /// <creator>wangl</creator>
        /// <date>2013-08-05</date> 
        /// </summary>
        public List<ElingOA6_Admin_DSJCJ.Model.Document> DataTableToList(DataTable dt)
        {
            List<ElingOA6_Admin_DSJCJ.Model.Document> modelList = new List<ElingOA6_Admin_DSJCJ.Model.Document>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                ElingOA6_Admin_DSJCJ.Model.Document model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = documentDAL.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
          /// <summary>
        /// 获得某一类别的已置顶的数据列表，字段 FTitle，FDocId
        /// <creator>wangl</creator>
        /// <date>2013-08-20</date> 
        /// </summary>
        public DataTable GetIsTopList(string docClsCd, string entityNo)
        {
            return documentDAL.GetIsTopList(docClsCd, entityNo).Tables[0];
        }
        /// <summary>
        /// 信息置顶排序
         /// <creator>wangl</creator>
        /// <date>2013-08-20</date> 
        /// </summary>
        /// <param name="docIdList">信息列表</param>
        public void UpdateDocTopSeq(List<decimal> docIdList)
        {
            documentDAL.UpdateDocTopSeq(docIdList);
        }
        /// <summary>
        /// 是否可以删除
        /// 未审批并且不在回收站
        /// <creator>wangl</creator>
        /// <date>2013-08-20</date> 
        /// </summary>
        /// <param name="docId">信息编号</param>
        /// <returns>true或者false</returns>
        public bool isCanDelte(decimal docId)
        {
            return documentDAL.isCanDelte(docId);
        }
        /// <summary>
        /// 信息放入回收站
        /// <creator>wangl</creator>
        /// <date>2013-08-20</date> 
        /// </summary>
        /// <param name="docIdList">信息编号列表</param>
        /// <param name="delEmplNo">删除员工编号</param>
        public void UpdateDocumentRecycle(List<decimal> docIdList, string delEmplNo)
        {
            documentDAL.UpdateDocumentRecycle(docIdList, delEmplNo);
        }

        /// <summary>
        /// 获得所有需要考核的信息(已录用，未删除，信息考核分已定义)
        /// <creator>sunlj</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        /// <param name="strEntityNo">单位编号</param>
        /// <param name="decYear">年度</param>
        /// <param name="strQuarter">季度</param>
        /// <returns></returns>
        public List<Model.Document> getDocByScore(string strEntityNo, decimal decYear, string strQuarter)
        {
            DataSet ds = documentDAL.getDocByScore(strEntityNo, decYear, strQuarter);
            return DataTableToListPart(ds.Tables[0]);
        }

        /// <summary>
        /// 获得某几个字段的数据列表 FDocId,FDocClsCd,FDeptNo,FEmplNo,FEmpName
        /// <creator>sunlj</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        public List<Model.Document> DataTableToListPart(DataTable dt)
        {
            List<Model.Document> modelList = new List<Model.Document>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.Document model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.Document();

                    if (dt.Columns.Contains("FDocId"))
                    {
                        if (dt.Rows[n]["FDocId"].ToString() != "")
                        {
                            model.FDocId = decimal.Parse(dt.Rows[n]["FDocId"].ToString());
                        }
                    }
                    if (dt.Columns.Contains("FDocClsCd"))
                    {
                        model.FDocClsCd = dt.Rows[n]["FDocClsCd"].ToString();
                    }
                    if (dt.Columns.Contains("FDeptNo"))
                    {
                        model.FDeptNo = dt.Rows[n]["FDeptNo"].ToString();
                    }
                    if (dt.Columns.Contains("FEmplNo"))
                    {
                        model.FEmplNo = dt.Rows[n]["FEmplNo"].ToString();
                    }
                    if (dt.Columns.Contains("FEmpName"))
                    {
                        model.FEmpName = dt.Rows[n]["FEmpName"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得所有需要考核的信息(包括未录用),主要用于统计投稿数量
        /// <creator>sunlj</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        /// <param name="strEntityNo">单位编号</param>
        /// <param name="decYear">年度</param>
        /// <param name="strQuarter">季度</param>
        /// <returns></returns>
        public List<Model.Document> getDocByScoreAll(string strEntityNo, decimal decYear, string strQuarter)
        {
            DataSet ds = documentDAL.getDocByScoreAll(strEntityNo, decYear, strQuarter);
            return DataTableToListPart(ds.Tables[0]);
        }

        /// <summary>
        /// 获取有合发情况的文档，只包括已录用的
        /// <creator>sunlj</creator>
        /// <creator>2013-10-16</creator>
        /// </summary>
        /// <param name="strEntityNo">单位编号</param>
        /// <param name="strQuarter">季度查询条件语句</param>
        /// <returns></returns>
        public List<Model.Document> GetDocByHF(string strEntityNo, string strQuarter)
        {
            DataSet ds = documentDAL.GetDocByHF(strEntityNo, strQuarter);
            return DataTableToListPart(ds.Tables[0]);
        }

        /// <summary>
        /// 获取有合发情况的文档,包括未录用,未删除
        /// <creator>sunlj</creator>
        /// <date>2103-10-17</date>
        /// </summary>
        public List<Model.Document> GetDocByHFAll(string strEntityNo, string strQuarter)
        {
            DataSet ds = documentDAL.GetDocByHFAll(strEntityNo, strQuarter);
            return DataTableToListPart(ds.Tables[0]);
        }

        /// <summary>
        /// 获取某部门的所有需要考核的信息(未放入回收站,已录用的数据)的某几个字段 FDocId,FDocClsCd,FDeptNo,FEmplNo,FTitle,FPubDate,FEmpName
        /// <creator>sunlj</creator>
        /// <date>2013-10-19</date>
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decYear"></param>
        /// <param name="strEmplNo"></param>
        /// <returns></returns>
        public List<Model.Document> getDocByChkInfoAuditEntryByDept(string strEntityNo, decimal decYear, string strDeptNo, string strWhere)
        {
            DataSet ds = documentDAL.getDocByChkInfoAuditEntryByDept(strEntityNo, decYear, strDeptNo, strWhere);
            return DataTableToListPart3(ds.Tables[0]);
        }

        /// <summary>
        /// 获得某几个字段的数据列表 FDocId,FDocClsCd,FDeptNo,FEmplNo,FTitle,FPubDate,FEmpName
        /// <creator>sunlj</creator>
        /// <date>2013-10-19</date>
        /// </summary>
        public List<Model.Document> DataTableToListPart3(DataTable dt)
        {
            List<Model.Document> modelList = new List<Model.Document>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.Document model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.Document();
                    if (dt.Rows[n]["FDocId"].ToString() != "")
                    {
                        model.FDocId = decimal.Parse(dt.Rows[n]["FDocId"].ToString());
                    }
                    model.FDocClsCd = dt.Rows[n]["FDocClsCd"].ToString();
                    model.FDeptNo = dt.Rows[n]["FDeptNo"].ToString();
                    model.FEmplNo = dt.Rows[n]["FEmplNo"].ToString();
                    if (dt.Rows[n]["FPubDate"].ToString() != "")
                    {
                        model.FPubDate = DateTime.Parse(dt.Rows[n]["FPubDate"].ToString());
                    }
                    model.FTitle = dt.Rows[n]["FTitle"].ToString();
                    model.FEmpName = dt.Rows[n]["FEmpName"].ToString();
                    model.FDocClsName = dt.Rows[n]["FDocClsName"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 更新数据的jcms信息
        /// <creator>wangl</creator>
        /// <date>2013-11-20</date> 
        /// <param name="docId">信息编号</param>
        /// <param name="isPubJCMS">是否推送到JCMS Y,N</param>
        /// <param name="parallelDocClsCd">信息类别对应JCMS编号</param>
        /// </summary>
        public void UpdateJcms(decimal docId, string isPubJCMS, string parallelDocClsCd)
        {
            documentDAL.UpdateJcms(docId, isPubJCMS, parallelDocClsCd);
        }

        /// <summary>
        /// 获得单个缩略图路径
        /// <creator>wangl</creator>
        /// <date>2013-09-06</date>
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="docId">信息编号</param>
        /// <returns>缩略图路径</returns>
        public string GetDocFThumbSrvFileName(string entityNo, decimal docId)
        {
            return documentDAL.GetDocFThumbSrvFileName(entityNo, docId);
        }

    }
}
